/*
 * @lc app=leetcode.cn id=1356 lang=typescript
 *
 * [1356] 根据数字二进制下 1 的数目排序
 */

// @lc code=start
function sortByBits(arr: number[]): number[] {
    const countOne = (n: number) => {
        return n
            .toString(2)
            .split("")
            .filter((i) => i === "1").length;
    };
    return arr.sort((a, b) => {
        let result = countOne(a) - countOne(b);

        return result === 0 ? a - b : result;
    });
}
// @lc code=end
